﻿# coding: utf-8
import wx
import wx.lib.agw.gradientbutton as GB

import EntityModel
import Model
import Skeleton
import SkeletonData

PS_PLAY = 1
PS_PAUSE = 2

class wxPanelAnimation(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, style = wx.BORDER)
        
        stAniList = wx.StaticText(self, -1, TD.ANIMATION_LIST)
        self._listAnimation = wx.ListCtrl(self, -1, size = (-1, 200), style = wx.LC_REPORT)
        self._listAnimation.InsertColumn(0, TD.ANIMATION_NAME)
        
        self._bmpPlay = IL.GetBitmap(IL.ID_IMAGE_PLAY, IL.IMAGE_BIG)
        self._bmpPause = IL.GetBitmap(IL.ID_IMAGE_PAUSE, IL.IMAGE_BIG)
        self._bnPlay = GB.GradientButton(self, -1, self._bmpPause, size = (48, 48))
        sizerPlay = wx.BoxSizer(wx.HORIZONTAL)
        sizerPlay.Add(self._bnPlay, 0, wx.EXPAND|wx.ALL, border = 5)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(stAniList, 0, wx.EXPAND|wx.ALL, border = 5)
        sizer.Add(self._listAnimation, 1, wx.EXPAND|wx.ALL, border = 5)
        sizer.Add(sizerPlay, 0, wx.EXPAND|wx.ALL, border = 5)
        self.SetSizer(sizer)
        
        self.Bind(wx.EVT_BUTTON, self.__OnBnClickPlay, self._bnPlay)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.__OnListAnimationSelected, self._listAnimation)
        
        self.UpdateUI()
        
    def PauseAnimation(self, pause):        
        if not MODEL_EDITOR._modelEntityHandle:
            return
            
        coreModelHandle = EntityModel.GetCoreModel(MODEL_EDITOR._modelEntityHandle)
        Model.PauseAnimation(coreModelHandle, pause)                    
        
        if pause:
            self._playerState = PS_PAUSE
            self._bnPlay._bitmap = self._bmpPause   
        else:
            self._playerState = PS_PLAY
            self._bnPlay._bitmap = self._bmpPlay    
        
    def __OnBnClickPlay(self, e):
        if not MODEL_EDITOR._modelEntityHandle:
            return
            
        if self._playerState == PS_PLAY:
            self.PauseAnimation(True)
        else:
            self.PauseAnimation(False)      
            
    def UpdateUI(self):
        self._listAnimation.DeleteAllItems()
        self.PauseAnimation(True)
        if MODEL_EDITOR._modelEntityHandle:           
            coreModelHandle = EntityModel.GetCoreModel(MODEL_EDITOR._modelEntityHandle)
            animationCount = Model.GetAnimationCount(coreModelHandle)

            if animationCount > 0:
                self._bnPlay.Enable(True)
            else:
                self._bnPlay.Enable(False)
                
            for i in xrange(0, animationCount):
                ret, skeletonHandle = Model.GetSkeleton(coreModelHandle, i)
                skeletonData = Skeleton.GetSkeletonData(skeletonHandle)
                name = SkeletonData.GetName(skeletonData)
                self._listAnimation.InsertStringItem(self._listAnimation.GetItemCount(), name.decode('gbk'))
        else:
            self._bnPlay.Enable(False)
                
    def __OnListAnimationSelected(self, e):
        idx = e.m_itemIndex
        if MODEL_EDITOR._modelEntityHandle:
            coreModelHandle = EntityModel.GetCoreModel(MODEL_EDITOR._modelEntityHandle)
            Model.UseAnimation(coreModelHandle, idx, 0)